<template>
  <div class="login-container">
    <img src="http://www.macrozheng.com/admin/static/img/login_center_bg.5307896.png" alt="" class="bg-banner" />
    <div class="login-form-layout">
      <div class="form-body">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm login-form">
          <div class="mall-logo">
            <span class="logo-icon">
              <i class="el-icon-s-shop"></i>
            </span>
          </div>
          <h2 class="title">mall-admin-web</h2>
          <el-form-item prop="username">
            <el-input placeholder="请输入账号" prefix-icon="el-icon-user-solid" v-model="ruleForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="ruleForm.password" show-password></el-input>
          </el-form-item>
          <div class="btn_spacing">
            <button class="btn btn-primary" @click.prevent="submitForm('ruleForm')">登录</button>
            <button class="btn btn-primary" @click.prevent="resetForm">重置</button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { fetchLoginInfo } from '../../api/api'
export default {
  name: 'Login',
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 7 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码名称', trigger: 'blur' },
          { min: 3, max: 9, message: '长度在 3 到 9 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          fetchLoginInfo({
            data: { ...this.ruleForm },
          }).then((res) => {
            if (res.code == '200') {
              this.$message.success(res.message)
              let token = res.data.tokenHead + res.data.token
              // 存储token值
              localStorage.setItem('token', token)
              this.$router.push('/layout').catch(() => {})
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    resetForm() {
      this.ruleForm.username = ''
      this.ruleForm.password = ''
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep .el-form-item__content {
  margin: 0 !important;
}
.login-container {
  height: 100vh;
  position: relative;
  background-position: 10px 10px;
}
.login-container .bg-banner {
  height: 50px;
  background: #409eff;
  position: absolute;
  width: 100%;
  height: auto;
  margin-top: 200px;
}
.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 360px;
  margin: 140px auto;
  border-top: 10px solid #409eff;
  box-shadow: 0 2px 12px 0 rgb(155, 155, 155);
  border-radius: 3px;
}
.form-body {
  padding: 20px;
  background: #fff;
  text-align: center;
}
.logo-icon i {
  color: rgb(64, 158, 255);
}
.el-icon-s-shop {
  font-size: 60px;
}
.login-form .title {
  margin: 20px;
  color: #409eff;
  font-size: 22px;
}
.b_spacing {
  margin-bottom: 20px;
}
.btn {
  border: none;
  background: none;
  padding: 10px 40px;
  color: #fff;
  border-radius: 6px;
  cursor: pointer;
  margin: 0 15px;
}
.btn.btn-primary {
  background: #409eff;
}
.btn_spacing {
  margin-bottom: 30px;
}
.errorInfo {
  color: red;
  text-align: left;
  font-size: 14px;
  height: 25px;
}
::v-deep .is-success .el-input__inner {
  border: 1px solid green !important;
}
</style>
